﻿using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public enum TaskType
{
    TomatoTask = 0,
    RestTask = 1,
}

public static class TestKey
{
    public const string TomatoTask = "Tomato";
    public const string NormalRestTask = "NormalRestTask";
    public const string NormalToSpecailRestTaskInterval = "NormalToSpecailRestTaskInterval";
    public const string SpecailRestTask = "SpecailRestTask";
}

public class TaskManager : Step 
{

    private List<Task> taskList = null;
    private string taskName = null;

    public override void Init()
    {
        if (taskList == null)
        {
            taskList = new List<Task>();
        }

        InitPrefs();

        this.taskName = null;

        if (GetTaskCount() == 0)
        {
            GoToNextTask();
        }
        
        AppInit.Instance.AddUpdateFunc(this, Update);
    }

    private void Update()
    {
        Task curTask = GetCurTask();
        if(curTask == null)
        {
            GoToNextTask();
            return;
        }

        var curTaskState = curTask.GetTaskStateType();
        switch(curTaskState)
        {
            case TaskStateType.CurUtilFinish:
                GoToNextTask();
                break;
            case TaskStateType.WholeTaskFinish:
                NoticeCtrl.Instance.Open("要继续开始新的任务吗？", (bool isYes) =>
                {
                    if (isYes) EnterNewTask();
                    else this.Exit();
                });
                break;
            case TaskStateType.BackMainPanel:
                this.Exit();
                break;
        }
    }

    private void InitPrefs()
    {
        if (!PlayerPrefs.HasKey(TestKey.TomatoTask))
        {
            PlayerPrefs.SetInt(TestKey.TomatoTask, 1800);
        }
        if (!PlayerPrefs.HasKey(TestKey.NormalRestTask))
        {
            PlayerPrefs.SetInt(TestKey.NormalRestTask, 300);
        }
        if (!PlayerPrefs.HasKey(TestKey.NormalToSpecailRestTaskInterval))
        {
            PlayerPrefs.SetInt(TestKey.NormalToSpecailRestTaskInterval, 4);
        }
        if (!PlayerPrefs.HasKey(TestKey.SpecailRestTask))
        {
            PlayerPrefs.SetInt(TestKey.SpecailRestTask, 1800);
        }
    }

    /// <summary>
    /// 重新开始新的任务
    /// </summary>
    public void EnterNewTask()
    {
        BeforeExit();
        Init();
    }

    public override void BeforeExit()
    {
        AppInit.Instance.RemoveUpdateFunc(this);
        SaveLog();
        taskList.Clear();
        CountDownPanel.Instance.Close();
    }

    private Task CreateTask(TaskType taskType)
    {
        Task task = null;
        if (taskType == TaskType.TomatoTask)
        {
            task = new TomatoTask(this);
        }
        else
        {
            task = new RestTask(this);
        }

        return task;
    }

    public void GoToNextTask()
    {
        Task curTask = GetCurTask();
        if (curTask != null && curTask.IsRuning())
        {
            Debug.LogError("前置任务正在进行");
            return;
        }

        Task task = null;
        if (curTask == null || curTask.GetTaskType() == TaskType.RestTask)
        {
            Debug.Log("Create  TaskType.TomatoTask ");
            task = CreateTask(TaskType.TomatoTask);
        }
        else
        {
            Debug.Log("Create  TaskType.RestTask ");
            task = CreateTask(TaskType.RestTask);
        }

        if (task != null)
        {
            taskList.Add(task);
            task.Start();
        }
    }

    //private int 


    public Task GetCurTask()
    {
        if (taskList.Count == 0)
        {
            return null;
        }

        return taskList[taskList.Count - 1];
    }

    public Task GetStartTask()
    {
        if (taskList.Count == 0)
        {
            return null;
        }
        return taskList[0];
    }

    public int GetTaskCount()
    {
        return taskList.Count;
    }

    public int GetTaskCount(TaskType taskType)
    {
        int count = 0;
        for (int i = 0; i < taskList.Count; ++i)
        {
            if (taskList[i].GetTaskType() == taskType)
            {
                ++count;
            }
        }
        return count;
    }

    public void SetTaskName(string taskName)
    {
        this.taskName = taskName;
    }

    private void SaveLog()
    {
        StringBuilder log = new StringBuilder();

        log.AppendFormat("任务名称 [{0}] \n", string.IsNullOrEmpty(this.taskName) ? "未命名" : this.taskName);

        for (int i = 0; i < taskList.Count; ++i)
        {
            log.Append(taskList[i].GetLog());
            log.Append("\n");
        }

        if (log.Length > 0) SaveHandle.WriteLog(System.DateTime.Now.Date.ToString("yyyy/MM/dd"), log.ToString());
    }
}
